
// A1_compute_export_prices
//==============================================================================

// Description: This file computes average export prices for each country that are
// then used in Figure 1

clear
clear matrix
set more off

cd "D:\data_replication"


// Import and format trade and income data
//------------------------------------------------------------------------------

use data\trade\EU_imports_cn8_plus_summary_USD.dta, clear

keep if year==2007
drop if price_ton_USD==.

gen price_log = log(price_ton_USD)
merge m:1 declarant year using data\gdp_per_capita\GDP_per_capita_declarant.dta
keep if _merge==3
drop _merge
gen gdp_capita_declarant_log = log(gdp_per_capita_defl_declarant)

merge m:1 partner year using data\gdp_per_capita\GDP_per_capita_defl.dta
keep if _merge==3
drop _merge
gen gdp_capita_partner_log = log(gdp_per_capita_defl)


// Construct average prices
//------------------------------------------------------------------------------

sort year cn8plus partner
by year cn8plus partner: egen mean_price = mean(price_ton_USD)
by year cn8plus partner: egen total_imports = sum(imports_year_USD)
gen share_imports = imports_year_USD/total_imports
gen price_weighted = share_imports*price_ton_USD
by year cn8plus partner: egen price_avg_weighted = sum(price_weighted)
sort year cn8plus partner
by year cn8plus partner: keep if _n==1
gen mean_price_log = log(mean_price)
gen price_avg_weighted_log = log(price_avg_weighted)


// Create exporter dummies
//------------------------------------------------------------------------------

forval i=1/999 {
display `i'
gen s`i' = 0
replace s`i' = 1 if partner == `i'
egen sum_s = sum(s`i')
if sum_s[1]==0 {
drop s`i'
}
drop sum_s
}


// Run regression and save output
//------------------------------------------------------------------------------

xtset cn8plus
xtreg price_avg_weighted_log s3-s825, fe vce(cluster cn8plus)
estout using statistics_and_graphs\export_prices\output_files\prices_exporter.txt, replace

